package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> query = new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }
    @Override
    public AdminPO adminLogin(String phone) {
        log.info("管理员登录：{}", phone);
        AdminPO adminPO= adminMapper.adminLogin(phone);
        return adminPO;
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        log.debug("根据id查询用户,repository层{}",id);
        return adminMapper.selectById(id);
    }

    @Override
    public List<AdminPO> pagesList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = assembleAdminQuery(query);
        //PageHelper.startPage 之间不能有代码
//        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminMapper.selectList(queryWrapper);
        System.out.println("---------------333------------------"+pos);
        //selectList 数据库操作 之间不能有代码
        return pos;
    }

    @Override
    public Long count(QueryWrapper queryWrapper) {
        log.debug("查询数量,service层{}",queryWrapper);
        return adminMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(AdminPO adminPO) {
        log.debug("保存用户,repository层{}",adminPO);
        adminMapper.insert(adminPO);
    }

    @Override
    public List<AdminPO> list() {
        log.debug("查询所有用户,repository层");
        List<AdminPO> list = adminMapper.AdminVOList();
        return list;
    }
    private QueryWrapper<AdminPO> assembleAdminQuery(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.eq("account_phone",query.getAccountPhone());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getNickname())){
            queryWrapper.like("nickname",query.getNickname());
        }
        return queryWrapper;
    }
}
